<!--
 * @Author: ChunLai
 * @Date: 2022-01-17 11:24:24
 * @LastEditTime: 2025-11-19 15:09:02
 * @Description: 订单确认页
 * @FilePath: \01.latest-short-insure\src\views\sign\bill.vue
-->
<template>
  <div class="bill_sure_page">
    <div class="check_status">
      <TipMsg msg="请认真核对投保信息，以防影响正常投保！" />
      <!-- 核保状态显示 -->
      <InsureStatus ref="insureFlagRef" />
    </div>
    <div class="mt10">
      <RiskPlanModule />
    </div>
    <!-- 投保信息 -->
    <InsureInfos />
    <ProtocolsReading id="protocols_module" />
    <!-- 签名客户列表 -->
    <template v-if="needSignName">
      <div class="notice-mode-head mt10">
        <span class="head-box">请完成以下签字</span>
      </div>
      <QianZiListModule ref="qianziList" id="sign_module" />
    </template>

    <FooterBtns
      btnNum="2"
      leftText="返回修改"
      @saveField="$router.push('/form')"
      :rightText="btnText"
      @saveForm="goHeBaoEvent"
    ></FooterBtns>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import RiskPlanModule from "@/components/RiskPlanModule";
import QianZiListModule from "./components/QianZiListModule";
import InsureInfos from "./components/InsureInfos";
import InsureStatus from "./components/InsureStatus";
import ProtocolsReading from "./components/ProtocolsReading";
import { underwrite, payment } from "@/utils/functions.js";
import { rebackUrlPath } from "@/methods/format.js";

export default {
  name: "billSurePage",
  data() {
    return {};
  },
  components: {
    RiskPlanModule,
    InsureInfos,
    InsureStatus,
    QianZiListModule,
    ProtocolsReading,
  },
  computed: {
    ...mapGetters({
      signDoneFlag: "home/signDoneFlag",
      signTarget: "page/signTarget",
      readFinish: "readings/wholeFinish",
      underwriteFlag: "insert/underwriteFlag",
    }),
    btnText() {
      let res = "去核保";
      if (["1", "4"].includes(this.underwriteFlag)) {
        res = "去支付";
      } else if (["5"].includes(this.underwriteFlag)) {
        res = "下一步";
      }
      return res;
    },
    needSignName() {
      let res = false;
      if (this.signTarget.length > 0) {
        res = true;
      }
      return res;
    },
  },
  watch: {},
  created() {
    this.$store.dispatch("readings/initFilesList");
  },
  mounted() {
    this.$refs.insureFlagRef.initModule();
    if (this.$getMessage("readFinish") == "1") {
      this.$store.dispatch("readings/setWholeFinish");
    }
  },
  methods: {
    goHeBaoEvent() {
      if (["5"].includes(this.underwriteFlag)) {
        return this.$router.push("/billres");
      }
      if (["libao"].includes(this.gysEngName)) {
        if (["1", "4"].includes(this.underwriteFlag)) {
          return this.goPayHandle();
        }
      }
      if (!this.readFinish) {
        let target = document.querySelector("#protocols_module");
        target.scrollIntoView({
          behavior: "smooth",
          block: "start",
        });
        return this.$toast("请完成以上条款的阅读");
      }
      if (this.needSignName && !this.signDoneFlag) {
        let target = document.querySelector("#sign_module");
        target.scrollIntoView({
          behavior: "smooth",
          block: "start",
        });
        return this.$toast("请完成以上签名");
      }
      this.$setMessage("readFinish", "1");
      // return this.$toast("开始核保。。。。。");
      this.underwriteApi();
    },
    underwriteApi() {
      this.$store.commit("updateLoad", true);
      this.$store.commit("updateLoadTxt", "正在核保中，请稍等");
      let params = {
        ...this.apiComParams,
      };
      underwrite(params).then(
        ({ data }) => {
          this.$store.commit("updateLoad", false);
          this.$store.commit("insert/SET_UNDERWRITE_FLAG", data.code || "0");
          this.$refs.insureFlagRef.initModule(); // 初始化保单状态提示
          if (data.contractNum) {
            this.$store.commit("SET_CONTRACT_NUM", data.contractNum);
          }
          if (!["0", "2"].includes(data.code)) {
            this.$store.commit("insert/SET_FORBID_ALTER", true);
          }
          this.$baseAlert(data.msg, () => {
            if (["1"].includes(data.code)) {
              if (data.payUrl) {
                window.location.replace(data.payUrl);
              }
            } else if (["5"].includes(data.code)) {
              this.$router.push("/billres");
            }
          });
        },
        (err) => {
          this.$store.commit("updateLoad", false);
        }
      );
    },
    // 支付
    goPayHandle() {
      let params = {
        ...this.apiComParams,
        successUrl: `${rebackUrlPath("#/billres")}`,
        failUrl: `${rebackUrlPath("#/billres")}`,
      };
      this.$store.commit("updateLoad", true);
      payment(params).then(
        ({ data }) => {
          this.$store.commit("updateLoad", false);
          if (data.code == "1") {
            if (data.url) {
              this.formChannelPay(data.url);
            }
          } else {
            this.$baseAlert(data.msg);
          }
        },
        (err) => {
          this.$store.commit("updateLoad", false);
        }
      );
    },
    formChannelPay(payUrl) {
      this.$nextTick(() => {
        var form = document.createElement("form");
        form.id = "ms-pay-form";
        document.body.appendChild(form);
        let input = document.createElement("input");
        input.type = "hidden";
        input.name = "requestDoc";
        // input.value = data;
        form.setAttribute("accept-charset", "gbk");
        form.appendChild(input);
        form.method = "GET";
        form.action = payUrl;
        form.submit();
      });
      document.body.removeChild(form);
    },
  },
};
</script>

<style lang="less" scoped>
.bill_sure_page {
  padding-bottom: 100px;
}
</style>
